package son.hut.vn;

import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.PolylineOptions;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class ShowDirection extends FragmentActivity{
	private EditText sourceAddress, desAddress;
	private Button btSearch;
	private DecodePolyline decodePolyline;  // lop nay giup tao url, lay danh sach cac diem ma duong di se di qua
	private JSONParser jsonParser;
	private GoogleMap googleMap;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.direction);
		
		decodePolyline = new DecodePolyline();
		jsonParser = new JSONParser();
		
		googleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapDirection)).getMap();
		
		sourceAddress = (EditText)findViewById(R.id.sourceAddress);
		desAddress = (EditText)findViewById(R.id.desAddress);
		
		btSearch = (Button)findViewById(R.id.search);
		btSearch.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//lay gia tri dau vao cua nguoi dung de thuc hien qua trinh search
				String source = sourceAddress.getText().toString().trim();
				String des = desAddress.getText().toString().trim();
				
				if(source!=null&& !source.equals("")&&des!=null&& !des.equals("")){
					//buoc1: dua vao dau vao de tao ra ban tin request (URL request)
					String urlRequest = decodePolyline.makeURL(source, des);
					
					//buoc2: dua vao url se lay ket qua tra ve la 1 file json
					String jsonResult = jsonParser.parserJson(urlRequest);
					
					//buoc3: neu file json tra ve !=null, thi se lay danh sach cac diem  roi ve cac doan duong, cac marker de tao ra duong di;
					if(jsonResult!=null){
						//goi phuong thuc de search
						drawPath(jsonResult);
					}
					
				}
			}
		});
	}
	
	//xay dung phuong thuc dua vao ket qua server tra ve la 1 file json de lay ra danh sach cac diem
	// dua vao danh sach nay de ve duong di
	private void drawPath(String json){
		try {
			JSONObject jsonObject = new JSONObject(json);
			JSONArray jsonArray = jsonObject.getJSONArray("routes");
			JSONObject jsonObject2 = jsonArray.getJSONObject(0);
			
			JSONObject jsonPolyline = jsonObject2.getJSONObject("overview_polyline");
			String encodePolyline = jsonPolyline.getString("points");
			
			//dua vao phuong thuc decodePoly() de lay danh sach cac diem di qua
			List<LatLng> listPoint = decodePolyline.decodePoly(encodePolyline);
			
			for (int i = 0; i < listPoint.size()-1; i++) {
				LatLng src = listPoint.get(i);
				LatLng dst = listPoint.get(i+1);
				
				googleMap.addPolyline(new PolylineOptions().add(src).add(dst).color(Color.RED).width(2));
				
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
}
